package com.telnetnz.ticketdesk.dao.wrapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;

import com.rockbb.common.dao.CacheManagerBean;
import com.telnetnz.ticketdesk.dao.entity.AttachmentEntity;
import com.telnetnz.ticketdesk.dao.mapper.AttachmentMapper;

public class AttachmentWrap
{
	public static final String[] ORDER_BY = {
		"create_time"
	};
	public static final String[] ORDER = {"DESC", "ASC"};

	private AttachmentMapper mapper;
	private Cache cache;

	public AttachmentWrap() {}

	public AttachmentWrap(AttachmentMapper mapper, CacheManagerBean cacheManagerBean)
	{
		this.mapper = mapper;
		this.cache = cacheManagerBean.get(mapper.getClass().getName());
	}

	/*
	 * Basic methods
	 */
	public AttachmentEntity select(long fileId)
	{
		return mapper.select(fileId);
	}

	public long countAll()
	{
		return mapper.countAll();
	}

	public void insert(AttachmentEntity entity)
	{
		mapper.insert(entity);
	}

	public void update(AttachmentEntity entity)
	{
		mapper.update(entity);
	}

	public void delete(AttachmentEntity entity)
	{
		mapper.delete(entity);
	}

	public Cache getCache()
	{
		return cache;
	}

	public List<Long> selectByPageAndCondition(
			int start,
			int limit,
			int orderby,
			int order,
			long ticketId,
			long commentId,
			long creatorId)
	{
		Map<String, Object> parameters = new HashMap<String, Object>();
		if (start < 0) {parameters.put("start", 0);}
		else {parameters.put("start", start);}

		if (limit <=0) {parameters.put("limit", 20);}
		else {parameters.put("limit", limit);}

		if (orderby > ORDER_BY.length - 1 || orderby < 0) {parameters.put("orderby", ORDER_BY[0]);}
		else {parameters.put("orderby", ORDER_BY[orderby]);}

		parameters.put("order", (order == 0) ? ORDER[0] : ORDER[1]);
		if (ticketId > -1L) parameters.put("ticket_id", ticketId);
		if (commentId > -1L) parameters.put("comment_id", commentId);
		if (creatorId > 0L) parameters.put("creator_id", creatorId);

		return mapper.selectByPageAndCondition(parameters);
	}

	public long countByCondition(
			long ticketId,
			long commentId,
			long creatorId)
	{
		Map<String, Object> parameters = new HashMap<String, Object>();
		if (ticketId > -1L) parameters.put("ticket_id", ticketId);
		if (commentId > -1L) parameters.put("comment_id", commentId);
		if (creatorId > 0L) parameters.put("creator_id", creatorId);

		return mapper.countByCondition(parameters);
	}
}
